package com.shine.app.commons;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.annotation.SuppressLint;

@SuppressLint("SimpleDateFormat")
public class StringUtils {
	private static Pattern emailer = Pattern
			.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
	private static SimpleDateFormat dateFormater = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");

	public static Date toDate(String sdate) {
		Date date = new Date();
		try {
			date = dateFormater.parse(sdate);
		} catch (ParseException e) {
		}
		return date;
	}

	public static boolean isEmpty(String input) {
		if (input == null || "".equals(input)) {
			return true;
		}
		for (int i = 0; i < input.length(); i++) {
			char c = input.charAt(i);
			if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
				return false;
			}
		}
		return true;
	}

	public static boolean isEmail(String email) {
		if (email == null || email.trim().length() == 0) {
			return false;
		}
		return emailer.matcher(email).matches();
	}

	public static int toInt(String str, int defValue) {
		try {
			return Integer.parseInt(str);
		} catch (Exception e) {
		}
		return defValue;
	}

	public static int toInt(Object object) {
		if (object == null)
			return 0;
		return toInt(object.toString(), 0);
	}

	public static long toLong(String object) {
		try {
			return Long.parseLong(object);
		} catch (Exception e) {
		}
		return 0;
	}

	public static boolean toBool(String string) {
		try {
			return Boolean.parseBoolean(string);
		} catch (Exception e) {
		}
		return false;
	}

	public static boolean regx(String string) {
		Pattern pattern = Pattern.compile("([a-z0-9]+)");
		Matcher matcher = pattern.matcher(string);
		return matcher.matches();
	}

	public static boolean reg(String string) {
		Pattern pattern = Pattern.compile("([0-9]+)");
		Matcher matcher = pattern.matcher(string);
		return matcher.matches();
	}

	public static String formatLength(int input) {
		String result = String.valueOf(input);
		result = String.format("%02d", result);
		return result;
	}

	public static String getHomePages(String homepage) {
		String result = "";
		if (StringUtils.isEmpty(homepage)) {
			return result;
		}
		boolean flag = homepage.contains("http://");
		if (flag) {
			result = homepage.replaceFirst("http://", "");
		} else {
			{
				result = homepage;
			}
		}
		return result;
	}
}